﻿using System.Web.UI.WebControls;
using EPiServer.Web.PropertyControls;
using System.Web.UI;
using System.Globalization;

namespace EPiServer.Municipality.Web.UI.PropertyControls {
    /// <summary>
    /// 
    /// </summary>
    /// <remarks></remarks>
    /// <example></example>
    public class PropertyGoogleMapsLocationControl : PropertyDataControl {

        /* *******************************************************************
        *  Properties 
        * *******************************************************************/
        #region private InputGoogleMapsLocation EditControl
        /// <summary>
        /// Get/Sets the EditControl of the PropertyGoogleMapsLocationControl
        /// </summary>
        /// <value></value>
        private InputGoogleMapsLocation EditControl {
            get;
            set;
        }
        #endregion

        /* *******************************************************************
        *  Methods 
        * *******************************************************************/
        #region public override void CreateDefaultControls()
        /// <summary>
        /// Creates the default controls.
        /// </summary>
        public override void CreateDefaultControls() {
            if (string.IsNullOrEmpty(ToString()))
                return;         
            
            string coordinates = ToWebString().Substring(ToWebString().IndexOf("(") + 1, ToWebString().IndexOf(")") - ToWebString().IndexOf("(") - 1);
            string[] splitCoordinates = coordinates.Split(new[] { ',' });

            double latitude;
            double longitude;
            double.TryParse(splitCoordinates[0], NumberStyles.Float, CultureInfo.GetCultureInfo("en-US"), out latitude);
            double.TryParse(splitCoordinates[1], NumberStyles.Float, CultureInfo.GetCultureInfo("en-US"), out longitude);

            ITextControl target = new Label { Text = "<img id='staticmap' src='http://maps.googleapis.com/maps/api/staticmap?zoom=14&size=480x240&maptype=roadmap&markers=color:red%7Ccolor:red%7Clabel:C%7C" + latitude.ToString().Replace(",", ".") + "," + longitude.ToString().Replace(",", ".") + "&sensor=false&scale=2' alt='Karta' />" };
            CopyWebAttributes((WebControl)target);
            Controls.Add((Control)target);
        }
        #endregion

        #region public override void ApplyEditChanges()
        /// <summary>
        /// 
        /// </summary>
        public override void ApplyEditChanges() {
            SetValue(EditControl.Value);
        }
        #endregion
        #region public override void CreateEditControls()
        /// <summary>
        /// 
        /// </summary>
        public override void CreateEditControls() {
            EditControl = new InputGoogleMapsLocation();
            EditControl.Value = PropertyData.Value as GoogleMapsLocation;
            ApplyControlAttributes(EditControl);
            Controls.Add(EditControl);
            SetupEditControls();
        }
        #endregion
    }
}